package com.plm.maque.controller;

import com.plm.maque.entity.User;
import com.plm.maque.service.ImageService;
import com.plm.maque.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Limi
 * @create 2019-04-21 18:30
 */
@RestController
public class ImageController {

    @Autowired
    private ImageService service;
    @Autowired
    private UserService userService;

    @PostMapping("/uploadImage")
    public String uploadImage(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("userid", 1);
        User user = userService.getUser(map);
        service.uploadImage(request, user);

        return "保存图片成功";
    }

    @GetMapping("/getImage/{id}")
    public void getImage(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            service.getImage(id, request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
